<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @changeForm="changeForm"
      @clear="clear"
    />
    <div v-if="meta.indexOf('ComboAdd') > -1" class="doumee-table-buttons">
      <el-button
        v-preventReClick
        type="primary"
        style="margin-right: 10px"
        @click="toVipCardAdd"
      >{{ translate("add") }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('tcname')"
        prop="name"
        min-width="200"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="detailClick(scope.row)">{{
            scope.row.name
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('cmoboType')"
        min-width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.mealsType == '0'">{{ translate("mpck") }}</span>
          <span v-if="scope.row.mealsType == '1'">{{
            translate("mpqxk")
          }}</span>
          <span v-if="scope.row.mealsType == '2'">{{ translate("ydck") }}</span>
          <span v-if="scope.row.mealsType == '3'">{{
            translate("courseSubCard")
          }}</span>
          <span v-if="scope.row.mealsType == '4'">{{
            translate("courseLimitCard")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('cs')"
        prop="playTimes"
        min-width="100"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.mealsType != "1" ? scope.row.nums : "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('indate')"
        prop="indate"
        min-width="200"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span v-if="scope.row.validityType == '1'">{{
            scope.row.indate
          }}</span>
          <span
            v-if="scope.row.validityType == '0'"
          >{{ scope.row.validityStartTime }} -
            {{ scope.row.validityEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('price')"
        prop="price"
        min-width="100"
      />
      <el-table-column
        align="center"
        :label="translate('zfxsl')"
        prop="circulationAll"
        min-width="100"
      >
        <template v-slot="scope">
          <span>{{ scope.row.circulationAll || translate("limitNot") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('yssl')"
        prop="sellNum"
        min-width="100"
      />
      <el-table-column
        align="center"
        :label="translate('saleChannel')"
        prop="channelName"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('saleSection')"
        min-width="300"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.saleDateStart + "~" + scope.row.saleDateEnd
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="发放数量" prop="circulationDay" min-width="100" show-overflow-tooltip /> -->
      <el-table-column
        align="center"
        :label="translate('status')"
        min-width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            :disabled="meta.indexOf('ComboStatus') == -1"
            @change="statusAction(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="translate('handle')"
        min-width="120"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="
                meta.indexOf('ComboAdd') > -1 &&
                  scope.row.status != '1' &&
                  scope.row.isVenue != '1'
              "
              v-preventReClick
              type="text"
              @click="editMeal(scope.row.id)"
            >{{ translate("edit") }}</el-button>
            <el-button
              v-if="meta.indexOf('ComboDel') > -1 && scope.row.status != '1'"
              v-preventReClick
              type="text"
              class="danger"
              @click="deleteMeal(scope.row.id)"
            >{{ translate("del") }}</el-button>
            <el-button
              v-if="meta.indexOf('ComboAdd') > -1 && scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="copy(scope.row.id)"
            >{{ translate("copy") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <ComboAdd
      v-if="isShowComboAdd"
      ref="ComboAddRef"
      @success="getList"
      @close="isShowComboAdd = false"
    />
    <ComboDetail
      v-if="isShowComboDetail"
      ref="ComboDetailRef"
      @close="isShowComboDetail = false"
    />
  </div>
</template>

<script>
import { mealList, venueProjectList, uploadFile } from '@/api'
import { parseTime } from '@/utils'
import { deleteMeal, mealDiscount, mealStatus } from '@/api/combo'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
import ComboAdd from '../comboAdd'
import ComboDetail from './detail.vue'
export default {
  name: 'Combo',
  components: {
    Pagination,
    ComboAdd,
    ComboDetail
  },
  data() {
    return {
      isShowComboAdd: false,
      isShowComboDetail: false,
      uploadFileUrl: uploadFile,
      queryFormConfig,
      uploadParam: {
        folder: 'MEALS_IMG'
      },
      isShow: false,
      meta: [],
      pagination: {
        page: 1,
        rows: 10
      },
      totalCount: 10,
      discountInfo: '',
      list: [],
      suitableVenue: [],
      listLoading: true,
      filters: {},
      excelPath: ''
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.initData()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    detailClick(row) {
      const { id } = row
      this.isShowComboDetail = true
      this.$nextTick(() => {
        this.$refs.ComboDetailRef.getTicketDetail(id)
        this.$refs.ComboDetailRef.isShowDrawer = true
      })
    },
    changeForm(e) {
      if (e === 'venueId') {
        this.$set(this.filters, 'projectId', '')
        const suitableVenue = this.suitableVenue
        let arr = []
        const obj = { ... this.queryFormConfig }
        suitableVenue.forEach(e => {
          if (e.value === this.filters.venueId) {
            arr = e.projects
            arr.forEach(s => {
              s.label = s.projectName
              s.value = s.id
            })
          }
        })
        obj.formItems[4].options = arr
        this.queryFormConfig = obj
      }
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    editMeal(id) {
      // 编辑
      this.isShowComboAdd = true
      this.$nextTick(() => {
        this.$refs.ComboAddRef.id = id
        this.$refs.ComboAddRef.copName = this.translate('editCombo')
        this.$refs.ComboAddRef.getMealInfo(id)
        this.$refs.ComboAddRef.isShowDrawer = true
      })
      // this.$router.push({ name: 'ComboAdd', query: { id }})
    },
    copy(id) {
      this.isShowComboAdd = true
      this.$nextTick(() => {
        this.$refs.ComboAddRef.id = ''
        this.$refs.ComboAddRef.getMealInfo(id)
        this.$refs.ComboAddRef.isShowDrawer = true
      })
    },
    statusAction(id, status) {
      mealStatus({ param: { id, status }}).then(res => {
        this.getList()
        if (res.errorCode === '000000') {
          this.$message.success(status === 0 || status === '0' ? this.translate('jyScs') : this.translate('qyScs'))
        }
      }, () => {
        this.getList()
      })
    },
    deleteMeal(id) {
      this.$confirm(this.translate('delComboPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          deleteMeal({ param: { id }}).then(res => {
            if (res.errorCode === '000000') {
              this.getList()
            }
          })
        })
        .catch(() => {
        })
    },
    initData() {
      venueProjectList({}).then((res) => {
        if (res.errorCode !== '000000') return
        this.suitableVenue = res.recordList
        const arr = res.recordList
        arr.forEach(e => {
          e.label = e.name
          e.value = e.id
        })
        this.queryFormConfig.formItems[3].options = arr
      })
    },
    venuechange() {
      this.$set(this.filters, 'projectId', '')
    },
    getList(page) {
      // 获取会员列表
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      mealList({
        pagination,
        param: { ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    toShow(scope) {
      // 展示折扣详情
      // list
      mealDiscount({ param: { id: scope.row.id }}).then(res => {
        if (res.errorCode === '000000') {
          this.discountInfo = res.record.discountInfo
        }
      })
    },
    toVipCardAdd() {
      this.isShowComboAdd = true
      this.$nextTick(() => {
        this.$refs.ComboAddRef.isShowDrawer = true
      })
      // this.$router.push({ name: 'ComboAdd' })
    },
    clear() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.pre {
  white-space: pre;
  .discount-title {
    color: #303133;
    font-size: 16px;
    margin-bottom: 12px;
  }
}
</style>
